import tkinter
import cv2
import os
from tkinter import messagebox
# 矩形画出坐标区域
# 创建主窗口
root = tkinter.Tk()
root.wm_attributes('-topmost', 0)  # 1置顶窗口。0恢复正常
root.title('Draw_Pos')
root.geometry('410x150')  # 适配坐标输入框的窗口大小


# 初始坐标 (left, top, width, height)
myPos = [300, 780, 1300, 100]
lPos = [430, 420, 520, 120]
rPos = [980, 420, 520, 120]
img_Path_00 = "../ScnPhoto/00.png"

# 绑定坐标变量（用于输入框交互）
my_left = tkinter.StringVar(value=str(myPos[0]))
my_top = tkinter.StringVar(value=str(myPos[1]))
my_width = tkinter.StringVar(value=str(myPos[2]))
my_height = tkinter.StringVar(value=str(myPos[3]))

l_left = tkinter.StringVar(value=str(lPos[0]))
l_top = tkinter.StringVar(value=str(lPos[1]))
l_width = tkinter.StringVar(value=str(lPos[2]))
l_height = tkinter.StringVar(value=str(lPos[3]))

r_left = tkinter.StringVar(value=str(rPos[0]))
r_top = tkinter.StringVar(value=str(rPos[1]))
r_width = tkinter.StringVar(value=str(rPos[2]))
r_height = tkinter.StringVar(value=str(rPos[3]))


def update_coordinates():
    """从输入框更新坐标值"""
    try:
        # 更新我的区域坐标
        myPos[0] = int(my_left.get())
        myPos[1] = int(my_top.get())
        myPos[2] = int(my_width.get())
        myPos[3] = int(my_height.get())

        # 更新左边区域坐标
        lPos[0] = int(l_left.get())
        lPos[1] = int(l_top.get())
        lPos[2] = int(l_width.get())
        lPos[3] = int(l_height.get())

        # 更新右边区域坐标
        rPos[0] = int(r_left.get())
        rPos[1] = int(r_top.get())
        rPos[2] = int(r_width.get())
        rPos[3] = int(r_height.get())
    except ValueError:
        messagebox.showerror("错误", "请输入有效的数字")


def NowPosArea():
    """绘制并显示当前坐标对应的矩形"""
    # 先更新坐标
    update_coordinates()

    # 检查图片是否存在
    if not os.path.exists(img_Path_00):
        messagebox.showerror("错误", f"图片不存在：{img_Path_00}")
        return

    # 读取图片
    img = cv2.imread(img_Path_00)
    if img is None:
        messagebox.showerror("错误", f"无法读取图片：{img_Path_00}")
        return

    # 绘制矩形
    colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)]  # 红、绿、蓝
    for pos, color in zip([myPos, lPos, rPos], colors):
        left, top, width, height = pos
        cv2.rectangle(img, (left, top), (left + width, top + height), color, 2)

    # 显示图片
    cv2.namedWindow('P_Esc', cv2.WINDOW_NORMAL)
    cv2.imshow('P_Esc', img)
    cv2.waitKey(0)#任意按键关闭窗口
    cv2.destroyAllWindows()


# 绑定窗口关闭事件：关闭时打印当前坐标
def on_closing():
    update_coordinates()  # 确保获取最新坐标
    # 打印坐标（格式：变量名 = (值)）
    print(f"myPos = {tuple(myPos)}")
    print(f"lPos = {tuple(lPos)}")
    print(f"rPos = {tuple(rPos)}")
    root.destroy()  # 销毁窗口

root.protocol("WM_DELETE_WINDOW", on_closing)  # 注册关闭事件回调


# 界面布局：坐标输入框
# 我的区域
tkinter.Label(root, text="我的区域").grid(row=0, column=0, padx=5, pady=5)
tkinter.Label(root, text="left:").grid(row=0, column=1)
tkinter.Entry(root, textvariable=my_left, width=6).grid(row=0, column=2)
tkinter.Label(root, text="top:").grid(row=0, column=3)
tkinter.Entry(root, textvariable=my_top, width=6).grid(row=0, column=4)
tkinter.Label(root, text="width:").grid(row=0, column=5)
tkinter.Entry(root, textvariable=my_width, width=6).grid(row=0, column=6)
tkinter.Label(root, text="height:").grid(row=0, column=7)
tkinter.Entry(root, textvariable=my_height, width=6).grid(row=0, column=8)

# 左边区域
tkinter.Label(root, text="左边区域").grid(row=1, column=0, padx=5, pady=5)
tkinter.Label(root, text="left:").grid(row=1, column=1)
tkinter.Entry(root, textvariable=l_left, width=6).grid(row=1, column=2)
tkinter.Label(root, text="top:").grid(row=1, column=3)
tkinter.Entry(root, textvariable=l_top, width=6).grid(row=1, column=4)
tkinter.Label(root, text="width:").grid(row=1, column=5)
tkinter.Entry(root, textvariable=l_width, width=6).grid(row=1, column=6)
tkinter.Label(root, text="height:").grid(row=1, column=7)
tkinter.Entry(root, textvariable=l_height, width=6).grid(row=1, column=8)

# 右边区域
tkinter.Label(root, text="右边区域").grid(row=2, column=0, padx=5, pady=5)
tkinter.Label(root, text="left:").grid(row=2, column=1)
tkinter.Entry(root, textvariable=r_left, width=6).grid(row=2, column=2)
tkinter.Label(root, text="top:").grid(row=2, column=3)
tkinter.Entry(root, textvariable=r_top, width=6).grid(row=2, column=4)
tkinter.Label(root, text="width:").grid(row=2, column=5)
tkinter.Entry(root, textvariable=r_width, width=6).grid(row=2, column=6)
tkinter.Label(root, text="height:").grid(row=2, column=7)
tkinter.Entry(root, textvariable=r_height, width=6).grid(row=2, column=8)

# Pos按钮（点击后显示当前坐标对应的矩形）
tkinter.Button(root, text="Pos", command=NowPosArea).grid(row=3, column=0, columnspan=9, pady=10)

# 启动主循环
root.mainloop()
